package com.jgzx.common;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.jgzx.utils.PortalEncryptUtil;


import java.io.Serializable;

/**
 * @Description 门户返回结果集
 * @Author chenzy
 * @Date 2021/9/8 15:11
 **/
public class PortalResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private int code = ResultCode.SUCCESS.getCode();

    /**
     * 返回数据
     */
    private T data;

    /**
     * 返回消息
     */
    private String msg;

    public PortalResult() {
    }

    private PortalResult(ResultCode code, T data, String msg) {
        this.code = code.getCode();
        this.data = data;
        this.msg = (StrUtil.isEmpty(msg) ? code.getMsg() : msg);
    }

    public boolean isSuccess() {
        return ResultCode.isSuccess(this.code);
    }

    public boolean isFail() {
        return !ResultCode.isSuccess(this.code);
    }

    public static <T> PortalResult<T> ok() {
        return new PortalResult(ResultCode.SUCCESS, null, ResultCode.SUCCESS.getMsg());
    }

    public static <T> PortalResult<T> ok(T data) {
        return new PortalResult(ResultCode.SUCCESS, data, ResultCode.SUCCESS.getMsg());
    }

    public static <T> PortalResult<T> ok(ResultCode resultCode) {
        return new PortalResult(resultCode, null, resultCode.getMsg());
    }

    public static <T> PortalResult<T> ok(String msg) {
        return new PortalResult(ResultCode.SUCCESS, null, msg);
    }

    public static <T> PortalResult<T> ok(T data, String msg) {
        return new PortalResult(ResultCode.SUCCESS, data, msg);
    }

    public static <T> PortalResult<T> fail(String msg) {
        return new PortalResult(ResultCode.FAILURE, null, msg);
    }

    public static <T> PortalResult<T> fail(ResultCode code) {
        return new PortalResult(code, null, code.getMsg());
    }

    public static <T> PortalResult<T> fail(ResultCode code, String msg) {
        return new PortalResult(code, null, msg);
    }

    public static <T> PortalResult<T> getPortalResult(String result, Class clazz) {
        PortalResult portalResult = JSONObject.parseObject(result, PortalResult.class);
        if (portalResult.getCode() == ResultCode.SUCCESS.getCode() && ObjectUtil.isNotNull(portalResult.getData())) {
            String decryptContent;
            try {
                decryptContent = PortalEncryptUtil.aesDecrypt(portalResult.getData().toString());
            } catch (Exception e) {
                return PortalResult.fail("解密错误");
            }
            if (ObjectUtil.isNotEmpty(decryptContent) && clazz != null) {
                portalResult.setData(JSONObject.parseObject(decryptContent, clazz));
            } else {
                try {
                    portalResult.setData(JSONObject.parseObject(decryptContent));
                } catch (Exception e) {
                    portalResult.setData(decryptContent);
                }
            }
        }
        return portalResult;
    }

    public int getCode() {
        return this.code;
    }

    public T getData() {
        return this.data;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setCode(final int code) {
        this.code = code;
    }

    public void setData(final T data) {
        this.data = data;
    }

    public void setMsg(final String msg) {
        this.msg = msg;
    }

}
